package com.asset.modules.system.controller;

import com.asset.modules.common.model.ParamBean;
import com.asset.modules.common.model.ResponseBean;
import com.asset.modules.common.utils.Constant;
import com.asset.modules.system.model.Role;
import com.asset.modules.system.service.UserService;
import com.asset.modules.system.service.impl.RoleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenchen
 */
@RestController
@CrossOrigin
@RequestMapping("/role")
@Api(tags = "角色信息接口")
public class RoleController {
    private static final Logger LOGGER = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    RoleServiceImpl roleService;

    @Autowired
    UserService us;

    @ApiOperation(value = "角色的保存和更新")
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    @NotNull
    public ResponseBean saveOrModify(@RequestBody Role role){
        try{
            roleService.saveOrUpdate(role);
            return new ResponseBean(HttpStatus.OK.value(), Constant.SUCCESS_REQUEST, true);
        }catch (Exception e){
            LOGGER.error(e.getMessage());
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "信息保存失败，请查看角色名称是否重复!", false);
        }
    }

    @ApiOperation(value = "根据ID获取角色")
    @RequestMapping(value = "/byId", method = RequestMethod.GET)
    @ResponseBody
    @NotNull
    public ResponseBean getRoleById(@RequestParam String id){
        Role role =  roleService.getRoleById(id);
        if(null==role){
            return new ResponseBean(HttpStatus.OK.value(),Constant.NO_RESULT , null);
        }
        return new ResponseBean(HttpStatus.OK.value(), Constant.SUCCESS_REQUEST, roleService.getRoleById(id));
    }


    @ApiOperation(value = "获取所有角色")
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean getRoles(){
        List<Role> roles =  roleService.getAll();
        if(roles.size()==0){
            return new ResponseBean(HttpStatus.OK.value(), Constant.NO_RESULT, null);

        }
        return new ResponseBean(HttpStatus.OK.value(), Constant.SUCCESS_REQUEST , roleService.getAll());
    }


    @ApiOperation(value = "删除角色")
    @RequestMapping(value = "/remove", method = RequestMethod.GET)
    @ResponseBody
    @NotNull
    public ResponseBean removeRoles( @RequestParam String ids){
        String[] removeIds = ids.split(",");
        List list = new ArrayList();
        for(String id : removeIds){
            boolean flag = us.isExist("role_id", id);
            if(flag){
              return  new ResponseBean(401, "待删除公司有关联用户数据，请先删除关联用户数据!", id);
            }
            list.add(id);
        }
        List<String> _ids = roleService.removeByIdsWithChecking(list);
        return new ResponseBean(HttpStatus.OK.value(), "操作成功！", _ids);
    }
}
